﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MSAcademic.v2.Infrastructure.Article;
using MSAcademic.v2.Infrastructure.Gallery;
using MSAcademic.v2.Web.Areas.Admin.Models.Article;

namespace MSAcademic.v2.Web.Areas.Admin.Controllers
{
    public partial class ArticleController : Controller
    {

        public ActionResult Index(int page)
        {
            var model = _articleIndexModelFactory();

            if (!model.CanView) { return RedirectToRoute("Index"); }

            model.Page = page;

            ViewData["CurrentView"] = "Articles";
            return View(model);
        }

        public ActionResult Create()
        {
            var model = _articleCreateModelFactory();

            if (!model.CanCreate) { return RedirectToRoute("ArticleIndexAdmin"); }

            return PartialView();
        }

        [HttpPost]
        public ActionResult Create(ArticleCreateModel model)
        {
            if (!ModelState.IsValid) { return View(model); }

            if (!model.CanCreate) { return RedirectToRoute("ArticleIndexAdmin"); }
            
            var articleId=_articleCommands.CreateArticle(model.Title, model.SignedInUser.Id);
            var galleryId=_galleryCommands.Create(model.Title, false);
            _galleryCommands.Attach(articleId,galleryId);
            return RedirectToRoute("ArticleIndexAdmin");
        }

        public ActionResult Update(int id)
        {
            var model = _articleUpdateModelFactory();
            model.Id = id;

            if (!model.CanUpdate) { return RedirectToRoute("ArticleIndexAdmin"); }
            
            model.Title = model.Article.Title;
            model.ShortText = model.Article.ShortText;
            model.LongText = model.Article.LongText;
            model.IsHidden = model.Article.IsHidden;
            model.PublishedOn = model.Article.PublishedOn;

            
            if (model.Article.PublishedOn.HasValue)
            {
                model.PublishOnUpdate = true;
            }
           

            ViewData["CurrentView"] = "Articles";
            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Update(ArticleUpdateModel model)
        {
            if (!ModelState.IsValid) { return View(model); }

            if (!model.CanUpdate) { return RedirectToRoute("ArticleIndexAdmin"); }
            //update the Article
            _articleCommands.UpdateArticle(model.Id, model.Title, model.ShortText, model.LongText, model.IsHidden, model.PublishOnUpdate,model.PublishedOn);
            
            //rebind tags to article if necessary
            if (!string.IsNullOrWhiteSpace(model.TagsToBindCsv))
            {
                var tagIds = model.TagsToBindCsv.Split(';').Select(x => Convert.ToInt32(x)).ToList();
                _articleCommands.BindTagsToArticle(model.Id, tagIds);
            }
            else
            {
                var tagIds = new List<int>();
                _articleCommands.BindTagsToArticle(model.Id, tagIds);
            }

            //bind subsections
            if(!string.IsNullOrWhiteSpace(model.SubSectionsToBindCsv))
            {
                var subsectionsIds = model.SubSectionsToBindCsv.Split(';').Select(m => Convert.ToInt32(m)).ToList();
                _articleCommands.BindSubsectionsToArticle(model.Id, subsectionsIds);
            }
            else
            {
                var subsectionsIds = new List<int>();
                _articleCommands.BindSubsectionsToArticle(model.Id, subsectionsIds);
            }
            return View(model);
        }

        [ValidateInput(false)]
        public ActionResult Preview(ArticlePreviewModel model)
        {
            if (!model.CanUpdate) { return RedirectToRoute("ArticleIndexAdmin"); }
            return View(model);
        }

        public bool Hide(int id)
        {
            //TODO: darko - vidit sta je ovo

            _articleCommands.HideArticle(id);
            return true;
        }

        public ActionResult ToggleHidden(int id, int page)
        {
            var model = _articleUpdateModelFactory();
            model.Id = id;

            if (!model.CanPublishAndHide) { return RedirectToRoute("ArticleIndexAdmin"); }

            _articleCommands.ToggleHideArticle(id);
            return RedirectToRoute("ArticleIndexAdmin", new { page });
        }

        public ActionResult Publish(int id, int page)
        {
            var model = _articleUpdateModelFactory();
            model.Id = id;

            if (!model.CanPublishAndHide) { return RedirectToRoute("Index"); }

            _articleCommands.PublishArticle(id);
            return RedirectToRoute("ArticleIndexAdmin", new { page });
        }
    }

    public partial class ArticleController
    {
        private readonly Func<ArticleIndexModel> _articleIndexModelFactory;
        private readonly Func<ArticleCreateModel> _articleCreateModelFactory;
        private readonly Func<ArticleUpdateModel> _articleUpdateModelFactory;
        private readonly IArticleCommands _articleCommands;
        private readonly IGalleryCommands _galleryCommands;

        public ArticleController(Func<ArticleIndexModel> articleIndexModelFactory,
                                 Func<ArticleCreateModel> articleCreateModelFactory,
                                 Func<ArticleUpdateModel> articleUpdateModelFactory,

                                 IArticleCommands articleCommands,
                                 IGalleryCommands galleryCommands)
        {
            _articleCreateModelFactory = articleCreateModelFactory;
            _articleIndexModelFactory = articleIndexModelFactory;
            _articleUpdateModelFactory = articleUpdateModelFactory;
            _galleryCommands = galleryCommands;
            _articleCommands = articleCommands;
        }

    }
}
